package com.qiantu.phone.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;

import com.amap.api.maps.model.LatLng;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.math.BigDecimal;
import java.util.List;

public class LocationUtil {
    public static String getLocationStr(LatLng latLng){
        BigDecimal latitude = new BigDecimal(latLng.latitude);
        BigDecimal longitude = new BigDecimal(latLng.longitude);

        //经纬度 保留小数点后6位
        return  latitude.setScale(6, BigDecimal.ROUND_HALF_UP).toString() + ":" +
                longitude.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String getLocationName(RegeocodeResult result){
        String locationName = result.getRegeocodeAddress().getDistrict();
        try {
            locationName =
                    result.getRegeocodeAddress().getDistrict()
                            + result.getRegeocodeAddress().getAois().get(0).getAoiName()
                            /*+ "|" + result.getRegeocodeAddress().getDistrict().replace("区", "")*/;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                locationName = locationName+result.getRegeocodeAddress().getTownship()/*+ "|"
                        + result.getRegeocodeAddress().getDistrict().replace("区", "")*/;
            }catch (Exception e1){
                locationName = locationName+result.getRegeocodeAddress().getTownship()/*+ "|"
                        + result.getRegeocodeAddress().getDistrict().replace("区", "")*/;
            }

        }
        return locationName;
    }

    public static Location getLastKnownLocation(Context context) {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
        LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = mLocationManager.getAllProviders();
        Location bestLocation = null;
        for (String provider : providers) {
            if(provider.equals("gps")){
                Location l = mLocationManager.getLastKnownLocation(provider);
                if (l == null) {
                    continue;
                }
                if (bestLocation == null) {
                    bestLocation = l;
                }
            }
        }
        return bestLocation;
    }

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     *
     * @param
     * @return true 表示开启
     */
    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }

        return false;
    }
}
